package com.shou.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.shou.BusinessException;
import com.shou.CommonVar;

@Controller("commonController")
@RequestMapping("/file")
public class CommonController extends MyController{
	
	/**
	 * fck编辑器上传图片
	 * 
	 * @param request
	 * @param cmfile
	 * @param fsize
	 * @param CKEditor
	 * @param CKEditorFuncNum
	 * @return
	 */
	@RequestMapping(value = "/fckupload")
	public ResponseEntity<String> fckupload(HttpServletRequest request,
			@RequestParam("upload") CommonsMultipartFile cmfile, Integer fsize,
			String CKEditor, Integer CKEditorFuncNum) {
		Integer filesize = fsize;
		if (fsize != null) {
			filesize = filesize * 1024 * 1024;
		} else {
			fsize = 10;
			filesize = 1024 * 1024 * 10;
		}

		StringBuffer sb = new StringBuffer();

		if (cmfile != null && !cmfile.isEmpty()) {
			try {
				if (cmfile.getSize() < filesize) {
					String filename = cmfile.getOriginalFilename();
					String suffix = filename.substring(filename
							.lastIndexOf("."));
					if ((".gif.png.jpg.jpeg.bmp".indexOf(suffix.toLowerCase()) >= 0)) {						
								
					} else {
						throw new BusinessException("文件格式错误");
					}
					String folder = "ckeditor";
					String fileName = com.shou.util.FileUtil.newFileName(cmfile.getOriginalFilename());
					SimpleDateFormat df = new SimpleDateFormat("yyyyMM"); 
					String path = df.format(new Date())  ;
					
					String filePath = CommonVar.FILE_PREFIX+"/"+folder+"/"+path;
					File file = new File(filePath);
					if(!file.exists()){
						file.mkdir(); //如果不存在则新建目录
					}
					filePath = filePath + "/"+fileName; 
					String imgUrl = CommonVar.IMG_URL+"/"+folder+"/"+path+"/"+fileName;
					File realfile = new File(filePath);
					if (!realfile.exists()) {
						realfile.createNewFile();
					}
					cmfile.getFileItem().write(realfile);
					sb.append("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction("
							+ CKEditorFuncNum
							+ ", '"
							+ imgUrl
							+ "' , '"
							+ "' , '" + "" + "');</script>");
				} else {
				}
			} catch (Exception e) {
				sb.append("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction("
						+ CKEditorFuncNum
						+ ", '' , '"
						+ "' , '"
						+ "上传失败"
						+ "');</script>");
			}
		}

		ResponseEntity<String> responseEntity = new ResponseEntity<String>(
				sb.toString(), initHttpHeaders(), HttpStatus.OK);
		return responseEntity;
	}
}
